##########################################################
#     prj:  TBtools plugin
#     date: 08 sep 2021
#     Author: Shawn Wang
##########################################################
## 
options(stringsAsFactors = F)
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
argv <- commandArgs(TRUE)
portal <- argv[1] # 'portal'
portal = as.numeric(portal)
options(shiny.port = portal)
if (!require('htmltools')) install.packages('htmltools')
if (!require('shiny')) install.packages('shiny')
library(shiny)

ui = shinyUI(
  bootstrapPage(
    h3("URL components"),
    verbatimTextOutput("urlText"),
  fluidPage(
  titlePanel(title = h4("Demonstration of renderPlot() -a histogram", align = "center")),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "vairable",label = "1.Select the varible from the iris dataset",
                  choices= c("Sepal.Length" = 1,"Sepal.Width" = 2,"Petal.Length" = 3,"Petal.Width" = 4),
                  selected = "Sepal.Length"),
      sliderInput(inputId = "BINs",label = "2.Select the number of BINs for histogram",
                  min = 5,
                  max = 25,
                  value = 15),
      radioButtons(inputId = "xxx", label = "3. Chose the bar color", 
                   choices = c("Red","Orange","Yellow","Blue"),
                   selected = "Blue"),
    ),
    mainPanel(
      plotOutput("myhist")
    )
  )
)
))


server = shinyServer(
  function(input, output,session){
    output$urlText <- renderText({
      paste(sep = "",
            "protocol: ", session$clientData$url_protocol, "\n",
            "hostname: ", session$clientData$url_hostname, "\n",
            "port: ",     session$clientData$url_port,     "\n"
      )
    })
    output$myhist <- renderPlot({
      colm <- as.numeric(input$vairable)
      hist(iris[,colm],breaks= seq(0, max(iris[,colm], l = input$BINs+1)),col = input$xxx)
    })

})


shinyApp(ui = ui,server = server)

